import logging

import allure
import jsonpath

from utils.send_request_utils import send_jdbc_query


@allure.step("3: http响应断言")
def http_assert(case,res,index=0):
    if case["check"]:
        check_info = jsonpath.jsonpath(res.json(), case["check"])[index]
        logging.info(f"3.HTTP响应断言内容：实际结果({check_info}) == 预期结果({case['expected']})")
        allure.attach(f"实际结果：{check_info} == 预期结果：{case['expected']}", name="HTTP响应断言内容")
        assert check_info == case["expected"]
    else:
        logging.info(f"3.HTTP响应断言内容：预期结果({case['expected']}) in 实际结果({res.text})")
        allure.attach(f"预期结果({case['expected']}) in 实际结果({res.text})", name="HTTP响应断言内容")
        assert case["expected"] in res.text

# @allure.step("3: 数据库断言")
def jdbc_assert(case):
    if case["sql_check"] and case["sql_expected"]:
        with allure.step("3: 数据库断言"):
            query_data = send_jdbc_query(case["sql_check"])
            logging.info(f"3.JDBC响应断言内容：实际结果({query_data}) == 预期结果({case['sql_expected']})")
            allure.attach(f"实际结果({query_data}) == 预期结果({case['sql_expected']})", name="JDBC响应断言内容")
            assert query_data == case["sql_expected"]